package com.servlet.salary;

import com.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet("/admin/listSalary")
public class ListSalaryServlet extends HttpServlet {
    //获取date参数，如果没有的话。。
    //获取deptid参数
    //获取id参数
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminService adminService=new AdminService();
        String year=req.getParameter("year");
        String month=req.getParameter("month");
        String deptid=req.getParameter("deptid");
        String id=req.getParameter("id");
        if (id=="")System.out.print("hah");
        System.out.print(deptid);
        System.out.print(year);
        /*for (Salary h : s) {
            System.out.println(h.getName()+" "+ h.getDeptid());
        }*/
        if(id!=""&&adminService.getEmployee(id)==null){
            req.setAttribute("Message","员工不存在，查找失败！");
            req.getRequestDispatcher("admingetfail.jsp").forward(req, resp);
            return;
        }
        if (deptid!=""&&!adminService.getDept(deptid)) {
            req.setAttribute("Message", "部门不存在，查找失败！");
            req.getRequestDispatcher("admingetfail.jsp").forward(req, resp);
            return;
        }
        Date date=new Date();
        SimpleDateFormat formattery = new SimpleDateFormat("yyyy");
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        String year_t=formattery.format(date);
        String month_t=formatter.format(date);

        if (year.equals("noyear")){
            System.out.println(year);
            if (deptid!=""){
                req.setAttribute("salarys",adminService.getSalaryByDept(deptid,year_t,month_t));
            }else if (id!=""){
                req.setAttribute("salarys",adminService.getSalaryByUser(id,year_t,month_t));
            }else{
                //List<Salary> salarys = adminService.getSalary(year_t,month_t);
                req.setAttribute("salarys",adminService.getSalary(year_t,month_t));
            }
        }else{
            if (month.equals("nomonth")){
                if (deptid!=""){
                    req.setAttribute("salarys",adminService.getSalaryByDeptYear(deptid,year));
                }else if (id!=""){
                    req.setAttribute("salarys",adminService.getSalaryByUserYear(id,year));
                }else{
                    req.setAttribute("salarys", adminService.getSalary(year));
                }
            }else{
                if (deptid!=""){
                    req.setAttribute("salarys", adminService.getSalaryByDept(deptid,year,month));
                }else if (id!=""){
                    req.setAttribute("salarys", adminService.getSalaryByUser(id,year,month));
                    req.getRequestDispatcher("admingsearchsalary.jsp").forward(req, resp);
                    adminService.getSalaryByUser(id,year,month);
                }else{
                    req.setAttribute("salarys", adminService.getSalary(year,month));
                }
            }
        }
        req.getRequestDispatcher("adminsearchsalary.jsp").forward(req, resp);
    }
}
